﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Shell;
using TFDP.Common;
using Microsoft.VisualStudio;
using TFDP.VisualStudioInterop;

namespace TFDP.VsPackage.Support
{
    public sealed class FileEditRequestManager
    {
        private IVsQueryEditQuerySave2 qeqsService;

        public FileEditRequestManager(IVsQueryEditQuerySave2 qeqsService)
        {
            this.qeqsService = qeqsService;
        }

        public FileEditRequestResult RequestEdit(string fileName)
        {
            if (qeqsService == null)
                return new FileEditRequestResult(false, "SVsQueryEditQuerySave service is not initialized");

            uint verdict = 0;
            uint moreInfo = 0;

            int result = ((IVsQueryEditQuerySave2)qeqsService).QueryEditFiles(
                (uint)(tagVSQueryEditFlags.QEF_SilentMode | tagVSQueryEditFlags.QEF_ImplicitEdit),
                1,
                new string[] { fileName },
                null,
                null,
                out verdict,
                out moreInfo
            );

            return new FileEditRequestResult(
                (result == VSConstants.S_OK),
                String.Format(
                    "{0} {1}",
                    VsInteropUtilities.QueryEditResultToString((tagVSQueryEditResult)verdict),
                    VsInteropUtilities.QueryEditResultFlagsToString((tagVSQueryEditResultFlags)moreInfo)
                )
            );
        }
    }
}
